$(document).ready(function() {		
		//如果某个保修单处于applied，则processed checkbox disabled
	
		$("#repairs tr :checkbox[name='processed']").each(function() {	
			var checked = $(this).siblings(":checkbox[name='applied']").attr("checked");
			if (checked) {
				
				$(this).attr("disabled", "disabled");
			}
			
			$(this).click(function() {
				var repairId = $(this).attr("value");
				var isProcessed = $(this).attr("checked");
				
				if (isProcessed) {
					var isConfirmed = confirm("你确定要受理ID：" + repairId + "的保修单吗？");
					if (isConfirmed) {
						return processRepair(repairId);
					} else {
						return false;
					}
				} else {
					isConfirmed = confirm("你确定要取消受理ID：" + repairId + "的保修单吗？");
					if (isConfirmed) {
						return cancelProcessRepair(repairId);
					} else {
						return false;
					}
				}
			})//end click;
		})//end each
		
		$("#repairs tr :checkbox[name='applied']").each(function() {			
			$(this).click(function() {
				var repairId = $(this).attr("value");
				var isApplied = $(this).attr("checked");
				
				if (isApplied) {
					var isConfirmed = confirm("你确定要处理ID：" + repairId + "的保修单吗？");
					if (isConfirmed) {
						var flag = applyRepair(repairId);
						if (flag) {
							$(this).siblings(":checkbox[name='processed']").attr("checked", "checked")
																		   .attr("disabled", "disabled");
						}
						return flag;
					} else {
						return false;
					}
				} else {
					isConfirmed = confirm("你确定要取消处理ID：" + repairId + "的保修单吗？");
					if (isConfirmed) {
						flag = cancelApplyRepair(repairId);
						if (flag) {
							$(this).siblings(":checkbox[name='processed']").removeAttr("disabled");
						}
					} else {
						return false;
					}
				}
			})//end click;
		})//end each
		
		$(":button[name='deleteRepair']").each(function(){
			
			$(this).click(function(){
				var repairId = $(this).val();
				
				var isConfirmed = confirm("你真的要删除ID:" + repairId + "的保修单吗？");
				if (isConfirmed) {
					var flag = deleteRepair(repairId);
					if (flag) {
						
						$(this).parent("td").parent("tr").remove();
					}
				} else {
					return false;
				}
			})//end click
		});//end each
		
		//购置单 删除按钮
		$(":button[name='deletePurchase']").each(function() {
			$(this).click(function() { 
				var purchaseItemId = $(this).val();
				var isConfirmed = confirm("你真的要删除ID：" + purchaseItemId + "的购置单吗？");
				
				if (isConfirmed) {
					var flag = deletePurchaseItem(purchaseItemId);
					if (flag) {
					//删除这一行
						$(this).parent().parent().remove();						
					}
				} else {
					//不删除
					return false;
				}
				
			});//end click
		});//end each
		
		$("#purchases :checkbox[name='processed']").each(function() {
			$(this).click(function() {
				var checked = $(this).attr("checked");
				var purchaseItemId = $(this).val();
				if (checked) {					
					var isConfirmed = confirm("你确定要受理ID:" + purchaseItemId + "的购置单吗?");				
					if (isConfirmed) {
						processPurchaseItem(purchaseItemId);							
					} else {
						return false;
					}
				} else {
					isConfirmed = confirm("你确定要取消受理ID:" + purchaseItemId + "的购置单吗？");
					if (isConfirmed) {
						cancelProcessPurchaseItem(purchaseItemId);
					} else {
						return false;
					}
				}
			});//end click
		});//end each
		
		$("#purchases :checkbox[name='applied']").each(function() {
			$(this).click(function() {
				var purchaseItemId = $(this).val();
				var checked = $(this).attr("checked");
				var status;
				if (checked) {
					status = confirm("你确定要处理ID:" + purchaseItemId + "的购置单吗?");
					if (status) {
						if(applyPurchaseItem(purchaseItemId)) {
							//成功处理成功，使processed checkbox disabled
							
							$(this).parent().prev().find(":checkbox").attr("disabled", "disabled").attr("checked", "checked");
						} else {
							return false;
						}
					} else {
						return false;
					}
				} else {
					status = confirm("你确定要取处理ID：" + purchaseItemId + "的购置单吗?");
					if (status) {
						if(cancelApplyPurchaseItem(purchaseItemId)) {
							$(this).parent().prev().find(":checkbox").removeAttr("disabled");
						} else {
							return false;
						}
					} else {
						return false;
					}
				}
				
			});//end click
		});//end each
		
		$("#purchases :checkbox[name='processed']").each(function() {
			var applied = $(this).parent().next().find(":checkbox").attr("checked");			
			if (applied) {
				$(this).attr("disabled", "disabled");
			}
		});//end each
})//end document ready
function processRepair(repairId) {
	var processStatus;
	//发出ajax请求，关闭对话框,成功选中checkbox否则显示错误信息
	$.ajax({
		type : "GET",
		url : "processRepair.do?id=" + repairId,
		dataType : "xml",
		async : false,
		success : function(xmlData) {
					$(".returnMessage").dialog("open");
					var data = $(xmlData);
					var status = data.find("process").text();
					
					if (status == "true") {
						processStatus = true;
						var message = data.find("message").text();
						
						$(".returnMessage").find("p").text(message);
						$(".returnMessage").dialog({
							autoOpen : false,
							modal : true,
							title : "受理成功",
							buttons : {
								"确定" : function() {$(this).dialog("close")}
							}
						});
						$(".returnMessage").dialog("open");
					} else {
						processStatus = false;
						var errorMessage = data.find("error-message").text();
						$(".returnMessage").find("p").text(errorMessage);
						$(".returnMessage").dialog({
							autoOpen : false,
							modal : true,
							title : "受理请求出错",
							buttons : {
								"确定" : function() {$(this).dialog("close")}
							}
						});
						
						$(".returnMessage").dialog("open");
					}
				}	
	});
	return processStatus;
};

function cancelProcessRepair(repairId) {
	
	var processStatus;
	$.ajax({
		type: "Get",
		dataType : "xml",
		async : false,
		url : "cancelProcessRepair.do?id=" + repairId,
		success : function(xmlData) {			
			var response = $(xmlData);
			var status = response.find("process").text();
			var message;
			var returnMessage = $(".returnMessage");
			if (status == "true") {
				processStatus = true;
				message = response.find("message").text();
				returnMessage.find("p").text(message);
				returnMessage.dialog({
					autoOpen : false,
					modal : true,
					title : "取消受理成功",
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
				returnMessage.dialog("open");
			} else {
				processStatus = false;
				message = response.find("error-message").text();
				returnMessage.find("p").text(message);
				returnMessage.dialog({
					autoOpen : false,
					modal : true,
					title : "取消受理失败",
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
				
				returnMessage.dialog("open");
			}
		}
	});
		
	return processStatus;
}
function applyRepair(repairId) {
	var applyStatus = false;
	$.ajax({
		type : "GET",
		url : "applyRepair.do?id=" + repairId,
		dataType : "xml",
		async : false,
		success : function(xmlData) {
			var response = $(xmlData);
			var status = response.find("apply").text();
			
			if (status == "true") {
				applyStatus = true;
				var message = response.find("message").text();
				$(".returnMessage").find("p").text(message);
				$(".returnMessage").dialog({
					autoOpen : false,
					modal : true,
					title : "处理成功",
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
				$(".returnMessage").dialog("open");
			} else {
				message = response.find("error-message").text();
				applyStatus = false;
				
				$(".returnMessage").find("p").text(message);
				$(".returnMessage").dialog({
					autoOpen : false,
					modal : true,
					title : "处理失败",
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
				$(".returnMessage").dialog("open");
			
			}
		}
	});
	
	return applyStatus;
}

function cancelApplyRepair(repairId) {
	var cancelStatus;
	
	$.ajax({
		type : "GET",
		dataType : "xml",
		url : "cancelApplyRepair.do?id=" + repairId,
		async : false,
		success : function(xmlData) {
			var returnMessage = $(".returnMessage");
			var response = $(xmlData);
			var status = response.find("apply").text();
			var message;
			if (status == "true") {
				cancelStatus = true;
				message = response.find("message").text();
				returnMessage.find("p").text(message);
				returnMessage.dialog({
					autoOpen : false,
					modal : true,
					title : "取消处理成功",
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
			} else {
				cancelStatus = false;
				message = response.find("error-message").text();
				returnMessage.find("p").text(message);
				returnMessage.dialog({
					autoOpen : false,
					modal : true,
					title : "取消处理失败",
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
			}//end if
			
			returnMessage.dialog("open");
		}
	});
	
	return cancelStatus;
}

function deleteRepair(repairId) {
	var deleteStatus;
	var returnMessage = $(".returnMessage");
	
	$.ajax({
		type : "GET",
		dataType : "xml",
		url : "deleteRepair.do?id=" + repairId,
		async : false,
		success : function(xmlData) {
			var response = $(xmlData);
			var status = response.find("delete").text();
			
			if (status == "true") {
				deleteStatus = true;
				var message = response.find("message").text();
				returnMessage.find("p").text(message);
				returnMessage.dialog({
					autoOpen : false,
					modal : true,
					title : "删除成功",
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
				returnMessage.dialog("open");
			} else {
				deleteStatus = false;
				message = response.find("error-message").text();
				returnMessage.find("p").text(message);
				returnMessage.dialog({
					autoOpen : false,
					modal : true,
					title : "删除失败",
					buttons : {
						"确定" : function() {
							$(this).dialog("close");
						}
					}
				});
				returnMessage.dialog("open");
			}
		}
	});
	return deleteStatus;
}

function processPurchaseItem(purchaseItemId) {
	var processStatus = false;
	var returnMessage = $(".returnMessage");
	
	$.ajax({
		type : "GET",
		dataType : "xml",
		async : false,
		url : "processPurchase.do?id=" + purchaseItemId,
		success : function(xmlData) {
			var response = $(xmlData); 
			var processed = response.find("process").text();
			var title;
			var message;
			
			if(processed == "true") {
				processStatus = true;
				title = "受理成功";
				message = response.find("message").text();				
				initialDialog(returnMessage, title, message)
				
			} else {
				processStatus = false;
				title = "受理失败";
				message = response.find("error-message").text();
				initialDialog(returnMessage, title, message);
			}
			returnMessage.dialog("open");
		}
	});//end ajax
	return processStatus;
}

function applyPurchaseItem(purchaseItemId) {
	
	var applyStatus = true;
	var returnMessage = $(".returnMessage");
	$.ajax({
		type : "GET",
		dataType : "xml",
		async : false,
		url : "applyPurchase.do?id=" + purchaseItemId,
		success : function(xmlData) {
			var response = $(xmlData);
			var applied = response.find("apply").text();
			var title;
			var message;
			if (applied == "true") {
				applyStatus = true;
				title = "处理成功";
				message = response.find("message").text();
				initialDialog(returnMessage, title, message);
			} else {
				applyStatus = false;
				title = "处理失败";
				message = response.find("error-message").text();
				initialDialog(returnMessage, title, message);
			}
			returnMessage.dialog("open");
		}
	});//end ajax
	return applyStatus;
}

function cancelProcessPurchaseItem(purchaseItemId) {
	var cancelProcessStatus = false;
	var returnMessage = $(".returnMessage");
	
	$.ajax({
		type : "GET",
		dataType : "xml",
		async : false,
		url : "cancelProcessPurchase.do?id=" + purchaseItemId,
		success : function(xmlData) {
			var response = $(xmlData);
			var status = response.find("process").text();
			var title;
			var message;
			if (status == "true") {
				cancelProcessStatus = true;
				title = "取消受理成功";
				message = response.find("message").text();
			} else {
				cancelProcessStatus = false;
				title = "取消受理失败";
				message = response.find("error-message").text();
			}
			initialDialog(returnMessage, title, message);
			returnMessage.dialog("open");
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			errorDialog(returnMessage, true);
		}
	});
	
	return cancelProcessStatus;
}

function cancelApplyPurchaseItem(purchaseItemId) {
	
	var cancelApplyStatus = false;
	var returnMessage = $(".returnMessage");
	try {
		$.ajax({
			type : "GET",
			dataType : "xml",
			async : false,
			url : "cancelApplyPurchase.do?id=" + purchaseItemId,
			success : function(xmlData) {
				var response = $(xmlData);
				var status = response.find("apply").text();
				var title;
				var message;
				if (status == "true") {
					cancelApplyStatus = true;
					title = "取消处理成功";
					message = response.find("message").text();
					initialDialog(returnMessage, title, message);
				} else {
					cancelApplyStatus = false;
					title = "取消处理失败";
					message = response.find("error-message").text();
					initialDialog(returnMessage, title, message);
					
				}
				returnMessage.dialog("open");
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				errorDialog(returnMessage, true);
			}
		});
	} catch (e) {
		errorDialog(returnMessage, true);
	}
	
	return cancelApplyStatus;
}

function deletePurchaseItem(purchaseItemId) {
	var deleteStatus = false;
	
	$.ajax({
		type : "GET",
		dataType : "xml",
		url : "deletePurchase.do?id=" + purchaseItemId,
		async : false,
		success : function(xmlData) {
			var response = $(xmlData);
			var status = response.find("delete").text();
			var object = $(".returnMessage");
			
			if (status == "true") {
				deleteStatus = true;
				var title = "删除成功";
				var message = response.find("message").text();
				initialDialog(object, title, message);
				object.dialog("open");
			} else {
				deleteStatus = false;
				title = "删除失败";
				message = response.find("error-message").text();
				initialDialog(object, title, message);
				object.dialog("open");
				
			}
		}//end success
	});//end ajax
	
	return deleteStatus;
}

function initialDialog(object, title, message) {
	object.find("p").text(message);
	object.dialog({
		autoOpen : false,
		modal : true,
		title : title,
		buttons : {
			"确定" : function() {
				$(this).dialog("close");
			}
		}
	});
}

function errorDialog(object, open) {
	initialDialog(object, "错误", "请求失败,请稍后再试");
	
	if(open) {
		object.dialog("open");
	}
}

